// pages/category/category.js
import Toast from '../../lib/vant-weapp/toast/toast';
const jeegot = require('../../utils/jeegot.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    loaded: false,
    active: 0,
    description: "",
    category: {},
    currentCategory: {},
    product: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let me = this
    jeegot.getProductCategroy({categoryId: options.id}).then(data =>{
      me.setData({category: data});
      wx.setNavigationBarTitle({
        title: me.data.category.title,
      })
      if(me.data.category.children != null && me.data.category.children.length>0) {
        me.setData({currentCategory: me.data.category.children[0]})
      }else{
        me.setData({currentCategory: me.data.category})
      }
      me.setData({description: me.data.currentCategory.desc ? me.data.currentCategory.desc : "" })
      this.getProductList()
    });
  },

  getProductList: function(){
    let me = this
    jeegot.getProductPageList(
      {categoryId: me.data.currentCategory.id, page:me.data.page}
    ).then(data =>{
      me.setData({product:data.records})
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    if(this.data.category.title != null) {
      wx.setNavigationBarTitle({
        title: this.data.category.title,
      })
    }

    let children = this.data.category.children;
    if(children && children.length>0 && children[0].desc != null){
      this.setData({
        description: this.data.category.children[0].desc
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeTab: function (e) {
    let index = e.detail.index;
    this.setData({
      description: this.data.category.children[index].desc,
      currentCategory: this.data.category.children[index],
      page:1
    })
    this.getProductList()
  },
  handleClick: function (e) {
    let id = e.currentTarget.dataset.value.id;
    wx.navigateTo({
      url: '/pages/product/product?id=' + id
    });
  },
  scrollListen: function (e) {
    let me = this
    if(this.data.page<=this.data.totalPage) {
      let cpage = me.data.page;
      me.setData({page: cpage + 1})
      me.getProductList()
    }else {
      //Toast("没有更多商品了")
      this.setData({loaded: true})
    }

  }
})